<?php
namespace app\index;
use think\Image;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class common 
{
    static private $export="";
    static private $Key="A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1,N1,O1,P1,Q1,R1,S1,T1,U1,V1,W1,S1,Y1,Z1";
    static private $Key2="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z";
    
    // 图片上传处理  曹振威
    public static function picture($files)
    {
        if(isset($files)){
          $info = $files->validate(['size'=>65678,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public'. DS .'uploads');
            if($info){  
                $a=$info->getSaveName();
                $imgp= str_replace("\\","/",$a);             
                $imgpath='uploads/'.$imgp;  
                return $imgpath;
            }else{  
             echo $files->getError();  
           } 
        }else{
            return array('status'=>-1,'msg'=>'没有获取到图片');
        }
    }
    
    // 验证手机号格式  曹振威
    public static function check_mobile($phone=0,$landline=0){
        if($phone != 0){
            preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $phone, $matches);
            if(!$matches){
                return array("ErrorCode"=>1 ,"ErrorMessage"=>"不是电话号码");
            }
        }
        
        if($landline != 0){
            preg_match("/^(\d{3}-)(\d{8})$|^(\d{4}-)(\d{7})$|^(\d{4}-)(\d{8})$/", $landline,$matches);
            if(!$matches){
                return array("ErrorCode"=>2 ,"ErrorMessage"=>"座机号码有误");
            }
        }
    }
    
    /**
     * 曹振威
     * 导出
     * @param type $key     名称 th
     * @param type $data    数据 td
     * @param type $keyname    顺序  对应data中的键名
     * @param type $filename   文件名  如订单列表
     * @param type $style   样式  如第一行全部加粗
     * @param type $setTitle   工作簿名称  如已支付
     */
    public static function output_excel($key,$data,$keyname,$filename,$style =array(),$setTitle='sheet1'){
        if(empty($data)){
            exit("没有数据，无需下载");
        } 
        self::$export=new Spreadsheet();            
        $arr= explode(",", self::$Key); // 截取th号
        $key=array_combine(array_values(array_slice( $arr ,0 , count($key) )) , $key); // 组合th
        $obj=self::$export->setActiveSheetIndex(0);
        foreach($key as $k =>$v){
            $obj-> setCellValue($k,$v);
            self::$export->getActiveSheet()->getStyle($k)->applyFromArray($style);
        }
        $arr= explode(",", self::$Key2); // tr
        $key=array_slice( $arr ,0 , count($key));
        foreach ($data as $k1=> $array){
            $obj=self::$export-> setActiveSheetIndex(0);
            $k2=0;
            foreach ($keyname as  $vv){
               $obj->setCellValue( $key[$k2].($k1+2) ,$array[$vv] );
               $k2++;
            }
        }
        $filename =$filename.'_'.date('Y-m-d H_i_s').".xlsx";
        self::$export->getActiveSheet()->setTitle($setTitle);
        self::$export->setActiveSheetIndex(0);
        header('Content-Type: applicationnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $writer = new Xlsx(self::$export);
        $writer->save('php://output');
    }
    
    //cookie加密函数
    public static function encryptCookie($value,$type=0){
        if(!$type){
        return base64_encode($value.C('ENCOOK_KEY'));
        }
        return str_replace(C('ENCOOK_KEY'),'',base64_decode($value));
    }
    
    public static function getfirstchar($s0){   
        $fchar = ord($s0{0});
        if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
//        $s1 = iconv("UTF-8","GBK", $s0);
//        $s2 = iconv("gb2312","UTF-8", $s1);
        $s1 = common::get_encoding($s0,'GB2312');  
        $s2 = common::get_encoding($s1,'UTF-8'); 
        if($s2 == $s0){$s = $s1;}else{$s = $s0;}
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if($asc >= -20319 and $asc <= -20284) return "A";
        if($asc >= -20283 and $asc <= -19776) return "B";
        if($asc >= -19775 and $asc <= -19219) return "C";
        if($asc >= -19218 and $asc <= -18711) return "D";
        if($asc >= -18710 and $asc <= -18527) return "E";
        if($asc >= -18526 and $asc <= -18240) return "F";
        if($asc >= -18239 and $asc <= -17923) return "G";
        if($asc >= -17922 and $asc <= -17418) return "H";
        if($asc >= -17417 and $asc <= -16475) return "J";
        if($asc >= -16474 and $asc <= -16213) return "K";
        if($asc >= -16212 and $asc <= -15641) return "L";
        if($asc >= -15640 and $asc <= -15166) return "M";
        if($asc >= -15165 and $asc <= -14923) return "N";
        if($asc >= -14922 and $asc <= -14915) return "O";
        if($asc >= -14914 and $asc <= -14631) return "P";
        if($asc >= -14630 and $asc <= -14150) return "Q";
        if($asc >= -14149 and $asc <= -14091) return "R";
        if($asc >= -14090 and $asc <= -13319) return "S";
        if($asc >= -13318 and $asc <= -12839) return "T";
        if($asc >= -12838 and $asc <= -12557) return "W";
        if($asc >= -12556 and $asc <= -11848) return "X";
        if($asc >= -11847 and $asc <= -11056) return "Y";
        if($asc >= -11055 and $asc <= -10247) return "Z";
        return null;
    }
 
    /** 
        * @name: get_encoding 
        * @description: 自动检测内容编码进行转换 
        * @param: string data 
        * @param: string to  目标编码 
        * @return: string 
       **/  
     public static function get_encoding($data,$to){  
           $encode_arr=array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');   
           $encoded=mb_detect_encoding($data, $encode_arr);   
           $data = mb_convert_encoding($data,$to,$encoded);   
           return $data;  
       }  
}

